package com.jzcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.enterprise.OrgRecordDTO;
import com.jzcn.common.dto.system.position.PositionRecordDTO;
import com.jzcn.common.dto.system.resources.ResourcesDTO;
import com.jzcn.common.dto.system.resources.SearchResourcesDTO;
import com.jzcn.common.enums.BaseStatusEnum;
import com.jzcn.common.enums.DeleteEnum;
import com.jzcn.common.exceptions.BusinessException;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.common.utils.JzPage;
import com.jzcn.domain.EntOrg;
import com.jzcn.service.SysResourcesService;
import com.jzcn.mapper.SysResourcesMapper;
import com.jzcn.domain.SysResources;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysResourcesServiceImpl extends BaseServiceImpl<SysResourcesMapper, SysResources> implements SysResourcesService {


    @Override
    public boolean create(ResourcesDTO sysResourcesDTO) {
        LambdaQueryWrapper<SysResources> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysResources::getParentId,sysResourcesDTO.getParentId());
        wrapper.eq(SysResources::getName,sysResourcesDTO.getName());
        if(super.count(wrapper) > 0){
            throw new BusinessException("菜单名称已存在");
        }
        sysResourcesDTO.setCreateTime(new Date());
        SysResources sysResources = new SysResources();
        BeanUtils.copyProperties(sysResourcesDTO,sysResources);
        return super.save(sysResources);
    }

    @Override
    public boolean update(ResourcesDTO sysResourcesDTO, Integer id){
        SysResources sysResources = new SysResources();
        BeanUtils.copyProperties(sysResourcesDTO,sysResources);
        sysResources.setId(id);
        return super.update(sysResources);

    }

    @Override
    public boolean delete(Integer id) {
        LambdaQueryWrapper<SysResources> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysResources::getParentId,id);
        if(super.count(wrapper) > 0){
            throw  new BusinessException("先移除子菜单");
        }
        return super.remove(id);
    }

    @Override
    public ResourcesDTO getInfo(Integer id) {
        SysResources sysResources = super.get(id);
        return BeanUtils.convert(sysResources, ResourcesDTO.class);
    }

    @Override
    public JzPage<ResourcesDTO> listPage(SearchResourcesDTO searchResourcesDTO) {
        IPage<SysResources> iPage = new Page<>(searchResourcesDTO.getPageNum(),searchResourcesDTO.getPageSize());
        LambdaQueryWrapper<SysResources> wrapper = new LambdaQueryWrapper<>();
        if(searchResourcesDTO.getParentId() != null){
            wrapper.eq(SysResources::getParentId,searchResourcesDTO.getParentId());
        }
        if(!StringUtils.isEmpty(searchResourcesDTO.getName())){
            wrapper.like(SysResources::getName,searchResourcesDTO.getName());
        }
        if(searchResourcesDTO.getStatus() !=null){
            wrapper.eq(SysResources::getStatus,searchResourcesDTO.getStatus());
        }
        wrapper.orderByAsc(SysResources::getSort);
        return BeanUtils.convertPage(new JzPage<>(baseMapper.selectPage(iPage,wrapper)),ResourcesDTO.class);
    }

    @Override
    public List<ResourcesDTO> treeData() {
        LambdaQueryWrapper<SysResources> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysResources::getStatus, BaseStatusEnum.START.getValue());
                wrapper.orderByAsc(SysResources::getSort);
        List<SysResources>  resourcesData= baseMapper.selectList(wrapper);
        return BeanUtils.convertList(resourcesData, ResourcesDTO.class);
    }

    @Override
    public boolean status(Integer id, Integer status) {
        SysResources sysResources = super.getById(id);
        sysResources.setStatus(status);
        return this.update(sysResources);
    }

    @Override
    public List<ResourcesDTO> currentUserTree(Integer userId) {
        List<SysResources> resourcesList = baseMapper.findResourcesListByUserId(userId);
        return BeanUtils.convertList(resourcesList,ResourcesDTO.class);
    }

}
